/*
 * @FileName: [PropertiesEncryptFactoryBean.java]
 * 
 * @Package com.bcinfo.upp.util
 * 
 * 
 * Copyright (c) 2011-2015 BCINFO Technology Limited Com.
 * All rights reserved.
 * 
 * This software is the confidential and proprietary
 * information of BCINFO Technology Limited Company
 * ("Confidential Information"). You shall not disclose
 * such Confidential Information and shall use it only
 * in accordance with the terms of the contract agreement
 * you entered into with RKY.
 * 
 * $Rev: 5 $
 * $LastChangedDate: 2014-06-11 16:24:56 +0800 (Wed, 11 Jun 2014) $
 * $LastChangedBy: huangb $
 * 
 * @category bcinfo-
 * 
 * @version 1.1
 * 
 * @author $Author: huangb $
 * 
 * Change History:[Formatter: author date description] <br/>
 * 1
 * 2
 * 3
 */

package com.bci.pwtz.common.util;

import java.util.Map;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.FactoryBean;

import com.bci.pwtz.exception.RCException;


@SuppressWarnings("rawtypes")
public class PropertiesEncryptFactoryBean implements FactoryBean
{
    private Properties _properties;

    private static final Logger _logger = Logger.getLogger(PropertiesEncryptFactoryBean.class);

    public Object getObject() throws Exception
    {
        return getProperties();
    }

    @SuppressWarnings("rawtypes")
    public Class getObjectType()
    {
        return java.util.Properties.class;
    }

    public boolean isSingleton()
    {
        return true;
    }

    public Properties getProperties()
    {
        return _properties;
    }

    public void setProperties(Properties properties) throws RCException
    {
        this._properties = properties;

        if (this._properties != null)
        {
            for (Map.Entry<Object, Object> entry : _properties.entrySet())
            {
                String propKey = (String) entry.getKey();
                String propValue = (String) entry.getValue();
                if (propValue != null)
                {
                    String propDeCryValue = null;
                    try
                    {
                        propDeCryValue = AESCryptor.decrypt(propValue);
                    }
                    catch (Exception ex)
                    {
                        _logger.error(ex);
                        throw new RCException(ex);
                    }
                    this._properties.put(propKey, propDeCryValue);
                }
            }
        }

    }
}
